package com.controller;

import com.entity.User;
import com.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Data
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    // 查询所有用户
    @GetMapping
    public List<User> selectAllUsers() {
        return userService.selectAllUsers();
    }

    @GetMapping("/{id}")
    // id 查询一个用户
    public User selectOneUserById(@PathVariable String id) {
        return userService.selectOneUserById(Integer.parseInt(id));
    }

    // name 查询用户
    @GetMapping(params = "name")
    public List<User> selectUsersByName(String name) {
        return userService.selectUsersByName(name);
    }

    // id 删除一个用户
    @DeleteMapping("/{id}")
    public int deleteOneUserById(@PathVariable String id) {
        return userService.deleteOneUserById(Integer.parseInt(id));
    }

    // 新增一个用户
    @PostMapping
    public int insertOneUserById(@RequestBody User user) {
        return userService.insertOneUserById(user);
    }

    // 修改一个用户
    @PutMapping
    public int updateOneUserById(@RequestBody User user) {
        return userService.updateOneUserById(user);
    }
}
